﻿/*********************************************************************
 版权所有: 版权所有(C) 2009 ,熙软研发部
 模块编号:  HospitalAdd.aspx
 作    者:高文华
 编写日期:2010-02-22
 审    核:
 审核日期:
 描    述:医院新增页面
...........................................................................
如果模块有修改，则每次修改添加以下注释(以系统上线后为基准)：

 *********************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using Cisoft.YiWu.Model;
using Cisoft.YiWu.BLL;
using Cisoft.IntegrationSystem.Model;
using Cisoft.IntegrationSystem.BLL;

public partial class Business_YiWu_HospitalAdd : WebUI.Page.BasePage
{
    private HospitalInfoMO amo = new HospitalInfoMO();
    private HospitalInfoBO abo = new HospitalInfoBO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            UserBind();
            Initializtion(this.ddl_ParentArea, 0, 0, "==请选择所属区域==");
        }
    }

    #region 新增事件
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        lblcheckArea.Text="";
        if (ddl_ParentArea.SelectedValue.ToString() == "0")
        {
            lblcheckArea.Text = "(*)请选择所属区域</span>";
            return;
        }

        int result = 0;
        string strErr = "";
        try
        {
            result = abo.Add(FillModel());
        }
        catch (Exception ex)
        {
            strErr = ex.Message;
        }
        if (result > 0)
        {
            PageRight("新增医院成功！", "HospitalManage.aspx", "点击返回", "");
        }
        else
        {
            PageError("新增医院失败！错误原因如下：" + strErr, "HospitalManage.aspx");
        }
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    private HospitalInfoMO FillModel()
    {
        int i = -1;
        amo.HI_HospitalID = abo.GetMaxId();
        amo.HI_HospitalName = tb_AreaName.Text;
        //判断区域编号是否存在
        //AreaNameIsExsist(tb_AreaName.Text.Trim());
        //amo.AreaSortName = tb_AreaShortName.Text.Trim();
        //判断区域简称是否存在
        //AreaShortNameIsExsist(tb_AreaShortName.Text.Trim());
        //amo.PlaceNames = tb_PlaceNames.Text.Trim();
        amo.HI_HospitalAreaID = int.TryParse(ddl_ParentArea.SelectedValue, out i) ? int.Parse(ddl_ParentArea.SelectedValue) : 0;
        amo.HI_HospitalOrder = int.Parse(tb_Weight.Text);

        amo.HI_HospitalLogo = "";

        //新逻辑，将滚动内容框中的内容设置为二进制入数据库防止样式单引号等对SQL语句的破坏---20081024  gwh---

        string strmiddle = txtContents.Text;

        byte[] c = System.Text.Encoding.UTF8.GetBytes(strmiddle);
        string strend = Convert.ToBase64String(c);

        amo.HI_HospitalSummary = strend;
        return amo;
    }

    /// <summary>
    /// 判断区域编号是否存在
    /// </summary>
    /// <param name="AreaName">区域编号（区域名称）</param>
    //private void AreaNameIsExsist(string AreaName)
    //{
    //    List<AreaMO> list = new AreaBO().GetModelList("");
    //    if (list.Count > 0)
    //    {
    //        foreach (AreaMO mo in list)
    //        {
    //            if (mo.AreaName.Trim() == AreaName)
    //            {
    //                PageError("该区域编号已经存在！", Request.RawUrl);
    //                break;
    //            }
    //        }
    //    }
    //}

    /// <summary>
    /// 判断区域简称是否存在
    /// </summary>
    /// <param name="AreaShortName">区域简称</param>
    //private void AreaShortNameIsExsist(string AreaShortName)
    //{
    //    List<AreaMO> list = new AreaBO().GetModelList("");
    //    if (list.Count > 0)
    //    {
    //        foreach (AreaMO mo in list)
    //        {
    //            if (mo.AreaSortName.Trim() == AreaShortName.Trim())
    //            {
    //                PageError("该区域简称已经存在！", Request.RawUrl);
    //                break;
    //            }
    //        }
    //    }
    //}

    #endregion

    #region 返回事件
    protected void butrReset_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("HospitalManage.aspx");
    }
    #endregion

    #region 业务员绑定
    private void UserBind()
    {
        //ddl_Leaders.Items.Add(new ListItem("请选择业务员", ""));
        //Cisoft.IntegrationSystem.BLL.UserBO ubo = new Cisoft.IntegrationSystem.BLL.UserBO();
        ////DataSet ds = ubo.GetUserList(
        //List<UserMO> userlist = new User_RelationsBO().GetSalesmanList();
        //if (userlist.Count > 0)
        //{
        //    ddl_Leaders.DataSource = userlist;
        //    ddl_Leaders.DataValueField = "US_UserID";
        //    ddl_Leaders.DataTextField = "US_UserName";
        //}
    }
    #endregion

    #region 区域绑定

    #region 下拉框显示树型

    /// <summary>
    /// 初始化组合框级别（无限级栏目）
    /// </summary>
    /// <param name="dropList">组合框名称</param>
    /// <param name="iDepth">深度</param>
    /// <param name="iParentID">父类ID</param>
    public void Initializtion(DropDownList dropList, int iDepth, int iParentID, string strTip)
    {
        dropList.Items.Clear();
        dropList.Items.Add(new ListItem(strTip, "0"));

        ShowColumns(dropList, iDepth, iParentID);
    }

    /// <summary>
    /// 组合框显示所有级别（无限级级别）
    /// </summary>
    /// <param name="dropList">组合框名称</param>
    /// <param name="iDepth">深度</param>
    ///  <param name="iParentID">父类ID</param>int iParentID
    public void ShowColumns(DropDownList dropList, int iDepth, int iParentID)
    {
        AreaInfoBO bllgroup = new AreaInfoBO();
        AreaInfoMO modelgroup = new AreaInfoMO();

        //定义变量
        int iCount = 0;
        int iSum = 0;
        string strGroupName = "";
        string strGroupID = "";


        DataSet myDataSet = new DataSet();

        myDataSet = bllgroup.GetList(" AI_AreaParentID =" + iParentID + "");
        iCount = myDataSet.Tables[0].Rows.Count;

        if (iCount != 0)
        {
            //iSum深度处理
            iSum = iDepth + 1;

            for (int i = 0; i < iCount; i++)
            {
                if (i == (iCount - 1))
                {
                    strGroupName = replaceWord(GetDepth("├◎" + myDataSet.Tables[0].Rows[i]["AI_AreaName"].ToString(), iSum));
                    strGroupID = myDataSet.Tables[0].Rows[i]["AI_AreaID"].ToString();

                    dropList.Items.Add(new ListItem(strGroupName, strGroupID));
                }
                else
                {
                    strGroupName = replaceWord(GetDepth("├◎" + myDataSet.Tables[0].Rows[i]["AI_AreaName"].ToString(), iSum));
                    strGroupID = myDataSet.Tables[0].Rows[i]["AI_AreaID"].ToString();

                    dropList.Items.Add(new ListItem(strGroupName, myDataSet.Tables[0].Rows[i]["AI_AreaID"].ToString()));
                }
                ShowColumns(dropList, iSum, Int32.Parse(myDataSet.Tables[0].Rows[i]["AI_AreaID"].ToString()));
            }
        }
    }

    /// <summary>
    /// 字符串插入"│"字符
    /// </summary>
    /// <param name="str">需要插入"│"的字符串</param>
    /// <param name="i">插入"│"字符串的索引位置</param>
    /// <returns></returns>
    private static string GetDepth(string strGroupName, int iCount)
    {
        for (int i = 1; i < iCount; i++)
        {
            strGroupName = strGroupName.Insert(0, "│");
        }

        return strGroupName;
    }

    /// <summary>
    /// 将指定字符串里的“&nbsp;”替换为空字符
    /// </summary>
    /// <param name="strText"></param>
    /// <returns></returns>
    private string replaceWord(string strText)
    {
        string outText = strText.Replace("&nbsp;", " ");
        return outText;
    }

    #endregion
    #endregion
}
